<?php

/*
 * User Object Model
 * Author: LiuYuanjun
 * E-Mail/MSN: lane@live.cn
 * QQ: 6879391
 */

class UserObj extends Obj {

    private $cookieName = 'Login';
    public $permissions = array();

//    protected static $_instance = array();
//
//    public static function I($primary='') {
//        if ($primary) {
//            if (!isset(self::$_instance[$primary])) {
//                self::$_instance[$primary] = new self($primary);
//            }
//            return self::$_instance[$primary];
//        } else {
//            return new self;
//        }
//    }

    public function _initialize($primary) {
        if ($primary === '_CURRENT_') {
            if (isset($_REQUEST['_logout']))
                $this->logout();
            if (isset($_REQUEST['_login']))
                $this->login();
            if ($this->checkCookie()) {
                $this->getPermissions();
                return true;
            }
            $this->showLogin();
        } else {
            parent::_initialize($primary);
        }
    }

    protected function login() {
        if ($_REQUEST['email'] && $_REQUEST['passwd']) {
            $this->dbData = $this->db->table($this->getTableName())->where(array('email', 'eq', trim($_REQUEST['email'])))->find();
            if ($this->dbData) {
                if ($this->passwd == md5($_REQUEST['passwd'])) {
                    if ($this->status != 1) {
                        $this->redirect('./', '该帐号尚未激活或已被封禁！', 2, 5);
                    }
                    cookie($this->cookieName, array($this->id, $this->passwd));
                    $this->login_count++;
                    $this->redirect('./', '登录成功，欢迎你 <b>' . $this->name . '</b>！');
                }
            }
            $this->redirect('./', '帐号或密码错误！', 2, 2);
        } else {
            $this->redirect('./', '请输入Email和登录密码！', 2, 2);
        }
        return false;
    }

    protected function checkCookie() {
        $loginCookie = cookie($this->cookieName);
        if (!empty($loginCookie)) {
            $this->primary = $loginCookie[0];
            if ($this->getDbData()) {
                if ($this->passwd == $loginCookie[1]) {
                    if ($this->status != 1) {
                        $this->redirect('./?_logout=1', '该帐号尚未激活或已被封禁！', 2, 5);
                    }
                    $this->last_visit = TS_NOW;
                    $this->last_ip = getClientIp();
                    self::$_instance[$this->primary] = $this;
                    return true;
                }
            }
        }
        return false;
    }
    
    protected function getPermissions() {
        global $JA_PERMISSION_CONFIG;
        $roles = $this->extended_role;
        $roles[] = $this->role;
        $this->permissions = Permission::get('r', $roles);
    }

    protected function logout() {
        cookie($this->cookieName, null);
        $this->redirect('./', '您已成功退出！');
    }

    protected function getTableName() {
        return 'user';
    }

    protected function getPrimaryField() {
        return 'id';
    }

    protected function isArray($field) {
        if (in_array($field, array('extended_role', 'permission')))
            return true;
        return false;
    }

    protected function showLogin() {
        include $this->tpl('login');
        exit;
    }

    //$type 1 success 2 fail 3 alert
    protected function redirect($url, $msg, $type=1, $wait=2) {
        include $this->tpl('redirect');
        exit;
    }

    protected function tpl($tpl) {
        return BASEPATH . 'tpl' . DS . $tpl . '.html';
    }

}

?>
